package com.woniuxy.oldobject.controller;

import com.woniuxy.oldobject.entity.NursingServer;
import com.woniuxy.oldobject.entity.ResponseEntity;
import com.woniuxy.oldobject.service.NursingServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author： 倪和楠
 * @Todo：
 * @Date： 2022/7/26 15:45
 * @Version： 1.0
 */
@RestController
@RequestMapping("/nuringserver")
public class NursingServerController {
    @Autowired
    NursingServerService nursingServerService;

    @GetMapping("/listallserver")
    public ResponseEntity listallserver(){
        List<NursingServer> alllist = nursingServerService.allserver();
        if (alllist != null) {
            return new ResponseEntity(alllist);
        }
        return ResponseEntity.ERROR;
    }
    @RequestMapping("/newserver")
    public ResponseEntity newserver(@RequestBody NursingServer nursingServer){
        boolean k = nursingServerService.newnurserver(nursingServer);
        if(k){
            return new ResponseEntity(200,"新增成功",k);
        }else{
            return ResponseEntity.ERROR;
        }
    }
    @RequestMapping ("/updataserver")
    public ResponseEntity updateserver(@RequestBody NursingServer nursingServer){
        boolean flag = nursingServerService.updateserver(nursingServer);
        if(flag){
            return  new ResponseEntity(200,"成功",flag);
        }else{
            return ResponseEntity.ERROR;
        }
    }

    @RequestMapping("/deleteserver/{id}")
    public ResponseEntity deleteserver(@PathVariable("id") Integer id) {
        int i = nursingServerService.deleteById(id);
        if (i > 0) {
            return new ResponseEntity(200, "删除成功", i);
        }
        return ResponseEntity.ERROR;
    }

    /**
     * 将护理服务绑定到护理服务项目下
     *
     * @param list
     * @return
     */
    @PostMapping("/insertWithId")
    public ResponseEntity insertServerWithProjectId(@RequestBody List<NursingServer> list) {
        int num = nursingServerService.insertServerWithProjectId(list);
        if (num != 0) {
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.ERROR;
    }

    /**
     * 通过projeId查询对应的护理服务
     *
     * @param projeId
     * @return
     */
    @GetMapping("/selectByProjectId/{projectId}")
    public ResponseEntity selectByProjectId(@PathVariable int projectId) {
        NursingServer nursingServer = new NursingServer();
        nursingServer.setProjectId(projectId);
        List<NursingServer> nursingServers = nursingServerService.queryAllByLimit(nursingServer);
        if (nursingServers != null) {
            return new ResponseEntity(nursingServers);
        }
        return ResponseEntity.ERROR;
    }
}

